//
//  LSMsgCell.m
//  LiveStar
//
//  Created by 漪珊 on 2016/12/22.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LSMsgCell.h"
#define MSG_TABLEVIEW_LABEL_FONT 14
static NSMutableArray      *_arryColor;
static NSInteger           _index = 1;
@implementation LSMsgCell
{
    UIView  *_msgView;
    UILabel *_msgLabel;
    UIImageView *_msgBkView;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _msgView  = [[UIView alloc] initWithFrame:CGRectZero];
        _msgBkView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"piece"]];
        
        [_msgView addSubview:_msgBkView];
        [_msgView sendSubviewToBack:_msgBkView];
        
        _msgView.layer.cornerRadius = 5;
        _msgView.layer.masksToBounds = YES;
        
        _msgLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _msgLabel.numberOfLines = 0;
        _msgLabel.font = UIFont(MSG_TABLEVIEW_LABEL_FONT);
        _msgBkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [_msgView addSubview:_msgLabel];
        [self.contentView addSubview:_msgView];
    }
    return self;
}

-(void)layoutSubviews{
    _msgLabel.frame = CGRectMake(10, 2, _msgLabel.width, _msgLabel.height+4);
    _msgView.frame  = CGRectMake(0, 0, _msgLabel.width + 20, _msgLabel.height+4);
    _msgBkView.frame = _msgView.frame;
}

-(void)refreshWithModel:(LSMsgModel *)msgModel{
    _msgLabel.attributedText = msgModel.msgAttribText;
    _msgLabel.width = MSG_TABLEVIEW_WIDTH - 20;
    [_msgLabel sizeToFit];
}

+(NSAttributedString *)getAttributedStringFromModel:(LSMsgModel *)msgModel{
    _arryColor = [[NSMutableArray alloc] initWithObjects:UIColorFromRGB(0x1fbcb6),UIColorFromRGB(0x2b7de2),UIColorFromRGB(0xff7906),nil];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] init];
    if (msgModel.msgType == TCMsgModelType_NormalMsg || msgModel.msgType == TCMsgModelType_DanmaMsg||msgModel.msgType == TCMsgModelType_RedPaper||msgModel.msgType == TCMsgModelType_Present)
    {
        NSTextAttachment *attch = [[NSTextAttachment alloc] init];
        switch ([msgModel.userLevel intValue]) {
            case 1:
                attch.image = [UIImage imageNamed:@"chatmsglevel_1"];
                attch.bounds = CGRectMake(0, 0, 14, 14);
                break;
            case 2:
                attch.image = [UIImage imageNamed:@"chatmsglevel_2"];
                attch.bounds = CGRectMake(0, 0, 14, 14);
                break;
            case 3:
                attch.image = [UIImage imageNamed:@"chatmsglevel_3"];
                attch.bounds = CGRectMake(0, 0, 14, 14);
                break;
            case 4:
                attch.image = [UIImage imageNamed:@"chatmsglevel_4"];
                attch.bounds = CGRectMake(0, 0, 14, 14);
                break;
            case 5:
                attch.image = [UIImage imageNamed:@"chatmsglevel_5"];
                attch.bounds = CGRectMake(0, 0, 14, 14);
                break;
            default:
                break;
        }
        ;
        
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
        [attribute appendAttributedString:string];
        NSMutableAttributedString *userName = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@ : ", msgModel.userName]];
        [attribute appendAttributedString:userName];
        
        NSMutableAttributedString *userMsg = [[NSMutableAttributedString alloc] initWithString:msgModel.userMsg];
        [attribute appendAttributedString:userMsg];
        
        [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:MSG_TABLEVIEW_LABEL_FONT] range:NSMakeRange(0,attribute.length)];
        
        _index = _index % [_arryColor count];
        [attribute addAttribute:NSForegroundColorAttributeName value:[_arryColor objectAtIndex:_index] range:NSMakeRange(0,userName.length)];
        [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(userName.length, userMsg.length+1)];
        _index++;
    }
    else
    {
        NSMutableAttributedString *msgShow = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"通知 %@%@", msgModel.userName, msgModel.userMsg]];
        [attribute appendAttributedString:msgShow];
        [attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:MSG_TABLEVIEW_LABEL_FONT] range:NSMakeRange(0, attribute.length)];
        [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:241/255.0 green:43/255.0 blue:91/255.0 alpha:1] range:NSMakeRange(0, msgShow.length)];
    }
    
    
    return attribute;
}

@end


/**
  观众list
 */
@implementation LSAudienceListCell



@end
